package com.jboom.templates.statemachine.spring.listener;

import com.jboom.templates.statemachine.spring.constant.OrderStatusEnum;
import com.jboom.templates.statemachine.spring.entity.Order;
import com.jboom.templates.statemachine.spring.event.OrderEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;

/**
 * @author zhangwen
 * @date 2023/5/25 13:03
 *
 * 处理监听到的 event
 */
@Slf4j
@WithStateMachine(name = "orderStateMachine")
public class OrderStateListener {

    @OnTransition(source = "WAIT_PAY",target = "WAIT_DELIVER")
    public boolean payTransition(Message<OrderEvent> message){
        Order order = (Order) message.getHeaders().get("order");
        order.setOrderStatus(OrderStatusEnum.WAIT_DELIVER);
        log.info("支付事件,状态机反馈信息，{}",message.getHeaders().toString());
        return true;
    }


    @OnTransition(source = "WAIT_DELIVER",target = "WAIT_RECEIVE")
    public boolean deliverTransition(Message<OrderEvent> message){
        Order order = (Order) message.getHeaders().get("order");
        order.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE);
        log.info("发货事件,状态机反馈信息，{}",message.getHeaders().toString());
        return true;
    }

    @OnTransition(source = "WAIT_RECEIVE",target = "FINISH")
    public boolean receiveTransition(Message<OrderEvent> message){
        Order order = (Order) message.getHeaders().get("order");
        order.setOrderStatus(OrderStatusEnum.FINISH);
        log.info("收货事件,状态机反馈信息，{}",message.getHeaders().toString());
        return true;
    }
}
